<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="renderer" content="webkit"/>
  <meta name="force-rendering" content="webkit"/>
  <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1"/>
  <title>广告类型</title>
  <!-- Tell the browser to be responsive to screen width -->
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">

  <link rel="stylesheet" href="/bower_components/bootstrap/dist/css/bootstrap.min.css">
  <link rel="stylesheet" href="/bower_components/font-awesome/css/font-awesome.min.css">
  <link rel="stylesheet" href="/bower_components/Ionicons/css/ionicons.min.css">
  <link rel="stylesheet" href="/bower_components/datatables.net-bs/css/dataTables.bootstrap.min.css">
  <link rel="stylesheet" href="/dist/css/AdminLTE.min.css">
  <link rel="stylesheet" href="/dist/css/skins/_all-skins.min.css">

  <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
  <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
  <!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
  <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
  <![endif]-->

  <!-- Google Font -->
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">

  <!--头部-->
  <header id="headerDiv" class="main-header">
    <!--#include  file="/pages/admin/header.html"-->
  </header>

  <!--左侧边栏 -->
  <aside id="sidebarDiv" class="main-sidebar">
    <!--#include  file="/pages/admin/sidebar.html"-->
  </aside>

  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <h1>
        <small>类型列表</small>
      </h1>
      <ol class="breadcrumb">
        <li><i class="fa fa-paste"></i> 模板管理</li>
        <li><a href="javascript:void(0);">类型列表</a></li>
      </ol>
    </section>


      <div class="container">
        <div class="row clearfix">
          <div class="col-md-12 column">
            <div class="modal fade" id="modal-container-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
              <div class="modal-dialog">
                <div class="modal-content">
                  <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h4 class="modal-title" id="myModalLabel">
                      新增
                    </h4>
                  </div>
                  <div class="box box-primary">
                    <!-- /.box-header -->
                    <div class="box-body">
                      <form id="dataForm" role="form">
                        <!-- text input -->
                        <input type="hidden" name="id" id="id"/>
                        <div class="form-group">
                          <label>广告类型名称</label>
                          <input id="name" name="name" type="text" class="form-control" placeholder="类型名称">
                        </div>
                        <div class="form-group">
                          <label>应用范围</label>
                          <div class="checkbox">
                            <label>
                              <input id="cpc" value="cpc" name="type_range" type="checkbox">
                              CPC
                            </label>
                          </div>
                          <div class="checkbox">
                            <label>
                              <input id="cpa" value="cpa" name="type_range" type="checkbox">
                              CPA
                            </label>
                          </div>
                          <div class="checkbox">
                            <label>
                              <input id="cpm"  value="cpm" name="type_range" type="checkbox">
                              CPM &nbsp;&nbsp;<span style="font-weight: 200; font-size: 12px;">用户千次浏览</span>
                            </label>
                          </div>
                          <div class="checkbox">
                            <label>
                              <input id="cpr"  value="cpr" name="type_range" type="checkbox">
                              CPR &nbsp;&nbsp;<span style="font-weight: 200; font-size: 12px;">按用户实际注册量结算</span>
                            </label>
                          </div>
                          <div class="checkbox">
                            <label>
                              <input id="cps"  value="cps" name="type_range" type="checkbox">
                              CPS &nbsp;&nbsp;<span style="font-weight: 200; font-size: 12px;">按用户充值给站长分成</span>
                            </label>
                          </div>
                        </div>
                        <div class="form-group">
                          <label>排序</label>
                          <input id="sort" name="sort" type="text" class="form-control" placeholder="排序">
                        </div>
                        <div class="form-group">
                          <label>说明</label>
                          <input id="intro" name="intro" type="text" class="form-control" placeholder="说明">
                        </div>
                      </form>
                    </div>
                    <!-- /.box-body -->
                  </div>
                  <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button> <button id="save" type="button" class="btn btn-primary">保存</button>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>


    <!-- Main content -->
    <section class="content">
      <div class="row">
        <div class="col-xs-12">
          <div class="box">
            <div class="box-header" style="padding-bottom: 0px;">
              <form role="form" class="form-inline pull-left">
                <button id="modal-1" onclick="add()" role="button"  data-toggle="modal" type="button" class="btn btn-success"><i class="fa fa-tumblr"></i>&nbsp;&nbsp;<span class="bold">新建类型</span></button>
              </form>
            </div>
            <div class="box-body table-responsive">
              <table id="datatable"  class="table table-bordered table-hover table-striped ">
                <thead>
                <tr>
                  <th style=" white-space:nowrap">ID</th>
                  <th style=" white-space:nowrap">广告类型名称</th>
                  <th style=" white-space:nowrap">应用范围</th>
                  <th style=" white-space:nowrap">排序</th>
                  <th style=" white-space:nowrap">说明</th>
                  <th style=" white-space:nowrap">状态</th>
                  <th style=" white-space:nowrap">操作</th>
                </tr>
                </thead>
                <tbody>
                </tbody>
              </table>
            </div>
            <!-- /.box-body -->
          </div>
          <!-- /.box -->
        </div>
      </div>
    </section>
    <!-- /.content -->
  </div>

  <!--底部-->
  <footer id="footerDiv" class="main-footer">
    <!--#include  file="/pages/admin/footer.html"-->
  </footer>

  <!-- Control Sidebar -->
  <!-- /.control-sidebar -->
  <!-- Add the sidebar's background. This div must be placed
       immediately after the control sidebar -->
  <div class="control-sidebar-bg"></div>
</div>
<!-- ./wrapper -->
<script src="/bower_components/jquery/dist/jquery.min.js"></script>
<script src="/bower_components/morris.js/morris.min.js"></script>
<script src="/bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="/bower_components/datatables.net/js/jquery.dataTables.min.js"></script>
<script src="/bower_components/datatables.net-bs/js/dataTables.bootstrap.min.js"></script>
<script src="/bower_components/jquery-slimscroll/jquery.slimscroll.min.js"></script>
<script src="/bower_components/fastclick/lib/fastclick.js"></script>
<script src="/dist/js/adminlte.min.js"></script>
<script src="/dist/js/store.min.js"></script>
<script src="/dist/js/layer/layer.js"></script>
<script src="/dist/js/bootstrapValidator.min.js"></script>


<script>
    var dataMap={};
    var datatable;
    $(function () {
        datatable = $("#datatable").DataTable({
            // 'scrollX' : $(document).width(),
            // 'aLengthMenu' : [10, 20, 250, 500],
            'scrollCollapse' : true,//
            'stateSave' : false,//保存配置状态
            'processing' : false, // 显示loading
            'paging' : true,
            'lengthChange' : false,
            'searching' : false,// 取消搜索框
            'ordering' : false,// 取消字段排序
            'info' : true,
            'autoWidth' : false,
            'deferRender': true,
            'serverSide' : true, // ajax请求时必须写本项
            // 'pagingType' : 'full_numbers',
            'ajax' : { // ajax请求
                'type' : "POST",
                'url' : "/lmgame/type/page",
                'data' : function ( d ) {
                    return d;
                },
                "error": function(xhr, ts, et) { //XMLHttpRequest, textStatus, errorThrown
                    if(xhr.status === 400){
                        window.location.href = "/pages/admin/login.html";
                    }
                    if(xhr.status === 403){
                        window.location.href = "/403.html";
                    }
                }
            },
            'columns' : [ // data数据绑定对应列
                {"data" : "id"},
                {"data" : "name"},
                {"data" : "type_range"},
                {"data" : "sort"},
                {"data" : "intro"},
                {"data" : "stats"},
                {
                    "class":          'notab',
                    "data":           null
                }
            ],
            "columnDefs" : [
                {
                    targets: 5,
                    render: function (a) {
                        if(a === 0){
                            return "正常";
                        }
                        if(a === 1){
                            return "锁定";
                        }
                    }
                },
                {
                    targets: 6,
                    render: function (a) {
                        let html = "";
                        if(a.stats === 0){
                            html += "<button onclick='updateStats("+a.id+",1)' type=\"button\" class=\"btn btn-primary btn-sm\">锁定</button>&nbsp;";
                        }else{
                            html += "<button onclick='updateStats("+a.id+",0)' type=\"button\" class=\"btn btn-primary btn-sm\">激活</button>&nbsp;";
                        }
                        html += "<button onclick='editForm("+a.id+")' type=\"button\" class=\"btn btn-default btn-sm\">编辑</button>&nbsp;";
                        html += "<button onclick='deleteConfirm("+a.id+")' type=\"button\" class=\"btn btn-danger btn-sm\">删除</button>";
                        return html;
                    }
                }
            ],
            "createdRow": function (row, data, index) {
                dataMap[data.id] = data;
            },
            'language' : {
                'lengthMenu' : "显示 _MENU_ 项结果", // 每页显示多少条
                'zeroRecords' : "没有记录！", // 没有记录时显示的内容
                'info' : "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项", // 当前页信息
                'infoEmpty' : "显示第 0 至 0 项结果，共 0 项", // 当前页信息-没有记录时
                'paginate' : {
                    'previous' : " << ",
                    'next' : " >> "
                }
            }
        });


        $('#dataForm').bootstrapValidator({
            message: 'This value is not valid',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                name: {
                    validators: {
                        notEmpty: {
                            message: '类型名不能为空'
                        }
                    }
                },
                sort: {
                    validators: {
                        notEmpty: {
                            message: '排序不能为空'
                        }
                    }
                }
            }
        });

        $("#save").click(function () {
            let url = "/lmgame/type/add";
            if($("#id").val() !== '') {
                url = "/lmgame/type/update";
            }else{
                let istrue = $("#dataForm").data("bootstrapValidator").isValid();
                if(!istrue){
                    return false;
                }
            }
            //表单提交的方法、比如ajax提交
            $.ajax({
                type: 'POST',
                url: url,
                data: $("#dataForm").serialize(),
                dataType: "json",
                success: function (data) {
                    if (data.code != 0) {
                        layer.msg(data.msg);
                    } else {
                        datatable.ajax.reload(false);
                    }
                    $("#modal-container-1").modal("hide");
                },
                error: function (xhr, ts, et) { //XMLHttpRequest, textStatus, errorThrown
                    if (xhr.status === 400) {
                        window.location.href = "/pages/admin/login.html";
                    }
                    if (xhr.status === 403) {
                        window.location.href = "/403.html";
                    }
                }
            });
        });
    });


    function search() {
        datatable.ajax.reload(false);
    }

    function clear() {
        $("#name").val("");
        $("#sort").val("");
        $("#intro").val("");
        $("#id").val("");
        $("#cpa").prop({checked:false});
        $("#cpc").prop({checked:false});
        $("#cpm").prop({checked:false});
        $("#cpr").prop({checked:false});
        $("#cps").prop({checked:false});
    }

    function add() {
        $("#myModalLabel").html("新增");
        clear();
        $("#modal-container-1").modal("show");
    }

    function editForm(id) {
        $("#myModalLabel").html("编辑");
        clear();
        let data = dataMap[id];
        $("#name").val(data.name);
        let range = data.type_range.split(",");
        $.each(range,function (index, d) {
            $("#"+d).prop({checked:true});
        });
        $("#sort").val(data.sort);
        $("#intro").val(data.intro);
        $("#id").val(data.id);
        $("#modal-container-1").modal("show");
    }


    function deleteConfirm(gameid) {
        layer.confirm('确定删除该类型吗？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            let url="/lmgame/type/delete";
            $.ajax({
                type : 'POST',
                url : url,
                data :{'id':gameid},
                dataType : "json",
                success: function(data) {
                    if(data.code == 0){
                        datatable.ajax.reload(false);
                        layer.msg('删除成功', {
                            icon: 1,
                            time: 1000 //3秒关闭（如果不配置，默认是3秒）
                        }, function(){
                            flag=true;
                        });
                    }else{
                        layer.msg(data.msg);
                    }
                },
                error: function (xhr, ts, et) { //XMLHttpRequest, textStatus, errorThrown
                    if (xhr.status === 400) {
                        window.location.href = "/pages/admin/login.html";
                    }
                    if (xhr.status === 403) {
                        window.location.href = "/403.html";
                    }
                }
            });
        }, function(){

        });
    }

    // 确认修改
    function updateStats(id, stats) {
        let msg = '确定锁定该类型吗？';

        if(stats == 0){
            msg = '确定激活该类型吗？';
        }

        layer.confirm(msg, {
            btn: ['确定','取消'] //按钮
        }, function(){
            let url="/lmgame/type/update";
            $.ajax({
                type : 'POST',
                url : url,
                data :{'id' : id, 'stats' : stats},
                dataType : "json",
                success: function(data) {
                    if(data.code == 0){
                        datatable.ajax.reload(false);
                        layer.msg('修改成功', {
                            icon: 1,
                            time: 1000 //3秒关闭（如果不配置，默认是3秒）
                        }, function(){
                            flag=true;
                        });
                    }else{
                        layer.msg(data.msg);
                    }
                },
                error: function (xhr, ts, et) { //XMLHttpRequest, textStatus, errorThrown
                    if (xhr.status === 400) {
                        window.location.href = "/pages/admin/login.html";
                    }
                    if (xhr.status === 403) {
                        window.location.href = "/403.html";
                    }
                }
            });
        }, function(){

        });
    }
</script>
</body>
</html>
